{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import librosa\n",
    "import glob"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "train = glob.glob('spectrogram-train/*.npy')\n",
    "x = []\n",
    "for fpath in train:\n",
    "    fpath = fpath.split('/')[1]\n",
    "    splitted = fpath.split('-')\n",
    "    if len(splitted) == 2:\n",
    "        splitted[1] = splitted[1].split('.')[1]\n",
    "        fpath = splitted[0] + '.' + splitted[1]\n",
    "    fpath = fpath.replace('.npy','.wav')\n",
    "    x.append('data/' + fpath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16341"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "augment = glob.glob('augment/*.wav')\n",
    "x.extend(augment)\n",
    "x = list(set(x))\n",
    "len(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_ = glob.glob('spectrogram-test/*.npy')\n",
    "test = []\n",
    "for t in test_:\n",
    "    f = t.split('/')[1].replace('.npy', '.wav')\n",
    "    test.append('data/'+f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 16341/16341 [15:07<00:00, 18.01it/s]\n",
      "100%|██████████| 560/560 [00:30<00:00, 18.51it/s]\n"
     ]
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "X = []\n",
    "for i in tqdm(range(len(x))):\n",
    "    y, sr = librosa.load(x[i], sr = 16000)\n",
    "    X.append(y)\n",
    "    \n",
    "Y = []\n",
    "for i in tqdm(range(len(test))):\n",
    "    y, sr = librosa.load(test[i], sr = 16000)\n",
    "    Y.append(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "\n",
    "with open('train-wav.pkl', 'wb') as fopen:\n",
    "    pickle.dump({'X': X, 'x': x}, fopen)\n",
    "    \n",
    "with open('test-wav.pkl', 'wb') as fopen:\n",
    "    pickle.dump({'Y': Y, 'y': test}, fopen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
